{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow基本概念\n",
    "\n",
    "介绍Tensorflow的基本概念，并且通过一个简单的线性回归例子把这些概念串联起来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.06369638]\n",
      " [-0.12739277]\n",
      " [-0.19108915]\n",
      " [-0.25478554]]\n",
      "2.8934655\n",
      "start training...\n",
      "2.8934655\n",
      "2.0777192\n",
      "1.5112712\n",
      "1.1178093\n",
      "0.84438056\n",
      "0.65424323\n",
      "0.5219023\n",
      "0.42966744\n",
      "0.36526385\n",
      "0.32017428\n",
      "0.2884886\n",
      "0.26610613\n",
      "0.25018114\n",
      "0.23873928\n",
      "0.23041058\n",
      "0.22424437\n",
      "0.21958086\n",
      "0.21596241\n",
      "0.21307142\n",
      "0.21068743\n",
      "0.20865744\n",
      "0.20687544\n",
      "0.20526776\n",
      "0.20378312\n",
      "0.20238619\n",
      "0.20105231\n",
      "0.19976427\n",
      "0.19851023\n",
      "0.19728197\n",
      "0.19607376\n",
      "0.19488154\n",
      "0.19370253\n",
      "0.19253482\n",
      "0.191377\n",
      "0.19022821\n",
      "0.18908764\n",
      "0.1879549\n",
      "0.18682961\n",
      "0.18571152\n",
      "0.18460044\n",
      "0.1834962\n",
      "0.18239874\n",
      "0.18130796\n",
      "0.1802238\n",
      "0.17914613\n",
      "0.17807499\n",
      "0.17701024\n",
      "0.17595187\n",
      "0.17489988\n",
      "0.17385413\n",
      "0.1728147\n",
      "0.17178147\n",
      "0.1707544\n",
      "0.16973343\n",
      "0.16871867\n",
      "0.16770987\n",
      "0.16670717\n",
      "0.16571048\n",
      "0.16471972\n",
      "0.16373488\n",
      "0.16275595\n",
      "0.16178286\n",
      "0.16081557\n",
      "0.1598541\n",
      "0.15889835\n",
      "0.1579483\n",
      "0.15700397\n",
      "0.15606527\n",
      "0.15513217\n",
      "0.15420465\n",
      "0.15328272\n",
      "0.15236624\n",
      "0.15145527\n",
      "0.15054974\n",
      "0.1496496\n",
      "0.1487549\n",
      "0.14786552\n",
      "0.14698143\n",
      "0.14610271\n",
      "0.14522918\n",
      "0.14436086\n",
      "0.14349774\n",
      "0.14263982\n",
      "0.14178698\n",
      "0.14093925\n",
      "0.14009658\n",
      "0.13925895\n",
      "0.13842636\n",
      "0.13759874\n",
      "0.13677604\n",
      "0.1359583\n",
      "0.13514543\n",
      "0.13433743\n",
      "0.13353422\n",
      "0.13273586\n",
      "0.13194223\n",
      "0.13115339\n",
      "0.13036925\n",
      "0.12958978\n",
      "0.12881501\n"
     ]
    }
   ],
   "source": [
    "from __future__ import print_function\n",
    "from __future__ import division\n",
    "\n",
    "import tensorflow as tf\n",
    "x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)\n",
    "y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)\n",
    "linear_model = tf.layers.Dense(units=1)\n",
    "\n",
    "y_pred = linear_model(x)\n",
    "sess = tf.Session()\n",
    "init = tf.global_variables_initializer()\n",
    "sess.run(init)\n",
    "\n",
    "print(sess.run(y_pred))\n",
    "\n",
    "loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)\n",
    "\n",
    "print(sess.run(loss))\n",
    "\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.01)\n",
    "train = optimizer.minimize(loss)\n",
    "print(\"start training...\")\n",
    "for i in range(100):\n",
    "  _, loss_value = sess.run((train, loss))\n",
    "  print(loss_value)\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3.6-env",
   "language": "python",
   "name": "py3.6-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
